package com.example.demo6.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.demo6.Service.ICategoryService;
import com.example.demo6.entity.Banner;
import com.example.demo6.entity.Category;
import com.example.demo6.utils.PageResult;
import com.example.demo6.utils.PassToken;
import com.example.demo6.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * categoryController
 * 
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult=categoryService.selectCategoryList(category,pageNum,pageSize);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }
    @PassToken
    @GetMapping("/list1")
    public ServerResponse list1(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult=categoryService.selectCategoryGoodsList(category,pageNum,pageSize);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出category列表
     */
//    @PreAuthorize("@ss.hasPermi('system:category:export')")
//    @Log(title = "category", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Category category)
//    {
//        List<Category> list = categoryService.selectCategoryList(category);
//        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
//        util.exportExcel(response, list, "category数据");
//    }

    /**
     * 获取category详细信息
     */
    @GetMapping(value = "/{cid}")
    public ServerResponse getInfo(@PathVariable("cid") Long cid)
    {
        Category category=categoryService.selectCategoryByCid(cid);
        return ServerResponse.createBySuccess(200,category,"查询成功");
    }

    /**
     * 新增category
     */
    @PostMapping
    public ServerResponse add(@RequestBody Category category)
    {
        int i=categoryService.insertCategory(category);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","添加成功");
        }else{
            return ServerResponse.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改category
     */
    @PutMapping
    public ServerResponse edit(@RequestBody Category category)
    {
        int i=categoryService.updateCategory(category);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","更新成功");
        }else{
            return ServerResponse.createByFail(500,"更新失败");
        }
    }

    /**
     * 删除category
     */
	@DeleteMapping("/{cids}")
    public ServerResponse remove(@PathVariable Long[] cids)
    {
        int i=categoryService.deleteCategoryByCids(cids);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","删除成功");
        }else{
            return ServerResponse.createByFail(500,"删除失败");
        }
    }
}
